/////////////////////////////////////////////////////////////////////////////
// Original code from libhdfs3. Copyright (c) 2013 - 2014, Pivotal Inc.
// All rights reserved. Author: Zhanwei Wang
/////////////////////////////////////////////////////////////////////////////
//  Modifications by Kumo Inc.
// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//



#ifndef _HDFS_LIBHDFS3_CLIENT_PACKETPOOL_H_
#define _HDFS_LIBHDFS3_CLIENT_PACKETPOOL_H_
#include <memory>
#include <kmhdfs/common/thread.h>

#include <deque>

namespace Hdfs {
namespace Internal {

class Packet;

/*
 * A simple packet pool implementation.
 *
 * Packet is created here if no packet is available.
 * And then add to Pipeline's packet queue to wait for the ack.
 * The Pipeline's packet queue size is not larger than the PacketPool's max size,
 * otherwise the write operation will be pending for the ack.
 * Once the ack is received, packet will reutrn back to the PacketPool to reuse.
 */
class PacketPool {
public:
    PacketPool(int size);
    std::shared_ptr<Packet> getPacket(int pktSize, int chunksPerPkt,
                                 int64_t offsetInBlock, int64_t seqno, int checksumSize);
    void releasePacket(std::shared_ptr<Packet> packet);

    void setMaxSize(int size) {
        this->maxSize = size;
    }

    int getMaxSize() const {
        return maxSize;
    }

private:
    int maxSize;
    std::deque<std::shared_ptr<Packet> > packets;
    mutex mut;
};

}
}

#endif /* _HDFS_LIBHDFS3_CLIENT_PACKETPOOL_H_ */
